<?php

/**
 * FaqItems actions.
 *
 * @package    sf_sandbox
 * @subpackage FaqItems
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class FaqItemsActions extends autoFaqItemsActions
{
  public function executeIndex()
  {
    return $this->forward('FaqItems', 'list');
  }

  public function executeList()
  {
    $this->processSort();

    $this->processFilters();


    // pager
    $this->pager = new sfPropelPager('FaqItems', 20);
    $c = new Criteria();
    $this->addSortCriteria($c);
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/faq_items')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/faq_items');
    }
  }

  public function executeCreate()
  {
    return $this->forward('FaqItems', 'edit');
  }

  public function executeSave()
  {
    return $this->forward('FaqItems', 'edit');
  }

  public function executeEdit()
  {
    $this->faq_items = $this->getFaqItemsOrCreate();

    if ($this->getRequest()->getMethod() == sfRequest::POST)
    {
      $this->updateFaqItemsFromRequest();
      $this->saveFaqItems($this->faq_items);
	
      $this->setFlash('notice', 'Thông tin đã được cập nhật');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('FaqItems/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('FaqItems/list');
      }
      else
      {
        return $this->redirect('FaqItems/edit?id='.$this->faq_items->getId());
      }
    }
    else
    {
     $this->type = $this->getRequestParameter('type');
      $this->labels = $this->getLabels();
    }
  }

  public function executeDelete()
  {
    $this->faq_items = FaqItemsPeer::retrieveByPk($this->getRequestParameter('id'));
    $this->forward404Unless($this->faq_items);

    try
    {
      if(!$this->deleteFaqItems($this->faq_items)){
      	 	$this->getRequest()->setError('delete', 'Không xóa được câu hỏi này vì tồn tại câu trả lời.');
      	 	return $this->forward('FaqItems', 'list');
      }
    }
    catch (PropelException $e)
    {
      $this->getRequest()->setError('delete', 'Could not delete the selected Faq items. Make sure it does not have any associated items.');
      return $this->forward('FaqItems', 'list');
    }

    return $this->redirect('FaqItems/list');
  }

  public function handleErrorEdit()
  {
    $this->preExecute();
    $this->faq_items = $this->getFaqItemsOrCreate();
    $this->updateFaqItemsFromRequest();

    $this->labels = $this->getLabels();

    return sfView::SUCCESS;
  }

  protected function saveFaqItems($faq_items)
  {
    $faq_items->save();

  }

  protected function deleteFaqItems($faq_items)
  {
    return $faq_items->delete();
  }

  protected function updateFaqItemsFromRequest()
  {
    $faq_items = $this->getRequestParameter('faq_items');

    if (isset($faq_items['title']))
    {
      $this->faq_items->setTitle($faq_items['title']);
    }
    if (isset($faq_items['parent_id']))
    {
      $this->faq_items->setParentId($faq_items['parent_id']);
    }
    if (isset($faq_items['cat_id']))
    {
      $this->faq_items->setCatId($faq_items['cat_id']);
    }
    if (isset($faq_items['user_id']))
    {
      $this->faq_items->setUserId($faq_items['user_id']);
    }
    if (isset($faq_items['content']))
    {
      $this->faq_items->setContent($faq_items['content']);
    }
    if (isset($faq_items['created_datetime']))
    {
      if ($faq_items['created_datetime'])
      {
        try
        {
          $dateFormat = new sfDateFormat($this->getUser()->getCulture());
                              if (!is_array($faq_items['created_datetime']))
          {
            $value = $dateFormat->format($faq_items['created_datetime'], 'I', $dateFormat->getInputPattern('g'));
          }
          else
          {
            $value_array = $faq_items['created_datetime'];
            $value = $value_array['year'].'-'.$value_array['month'].'-'.$value_array['day'].(isset($value_array['hour']) ? ' '.$value_array['hour'].':'.$value_array['minute'].(isset($value_array['second']) ? ':'.$value_array['second'] : '') : '');
          }
          $this->faq_items->setCreatedDatetime($value);
        }
        catch (sfException $e)
        {
          // not a date
        }
      }
      else
      {
        $this->faq_items->setCreatedDatetime(null);
      }
    }
    if (isset($faq_items['published']))
    {
      $this->faq_items->setPublished($faq_items['published']);
    }
    if (isset($faq_items['spam']))
    {
      $this->faq_items->setSpam($faq_items['spam']);
    }
  }

  protected function getFaqItemsOrCreate($id = 'id')
  {
    if (!$this->getRequestParameter($id))
    {
      $faq_items = new FaqItems();
    }
    else
    {
      $faq_items = FaqItemsPeer::retrieveByPk($this->getRequestParameter($id));

      $this->forward404Unless($faq_items);
    }

    return $faq_items;
  }

  protected function processFilters()
  {
  }

  protected function processSort()
  {
    if ($this->getRequestParameter('sort'))
    {
      $this->getUser()->setAttribute('sort', $this->getRequestParameter('sort'), 'sf_admin/faq_items/sort');
      $this->getUser()->setAttribute('type', $this->getRequestParameter('type', 'asc'), 'sf_admin/faq_items/sort');
    }

    if (!$this->getUser()->getAttribute('sort', null, 'sf_admin/faq_items/sort'))
    {
    }
  }

  protected function addFiltersCriteria($c)
  {
  	$c->add(FaqItemsPeer::PARENT_ID,0);
  }

  protected function addSortCriteria($c)
  {
    if ($sort_column = $this->getUser()->getAttribute('sort', null, 'sf_admin/faq_items/sort'))
    {
      $sort_column = FaqItemsPeer::translateFieldName($sort_column, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
      if ($this->getUser()->getAttribute('type', null, 'sf_admin/faq_items/sort') == 'asc')
      {
        $c->addAscendingOrderByColumn($sort_column);
      }
      else
      {
        $c->addDescendingOrderByColumn($sort_column);
      }
    }
  }

  protected function getLabels()
  {
    return array(
      'faq_items{id}' => 'Id:',
      'faq_items{title}' => 'Title:',
      'faq_items{parent_id}' => 'Parent:',
      'faq_items{cat_id}' => 'Cat:',
      'faq_items{user_id}' => 'User:',
      'faq_items{content}' => 'Content:',
      'faq_items{created_datetime}' => 'Created datetime:',
      'faq_items{published}' => 'Published:',
      'faq_items{spam}' => 'Spam:',
    );
  }
}